﻿using BizBrowser.Hander;
using BizBrowser.JS;
using BizBrowser.RPT;
using CefSharp;
using CefSharp.WinForms;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BizBrowser
{
    public partial class MainForm : Form
    {
        private ChromiumWebBrowser _browser;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            String page = string.Format(@"{0}\Html\TestFile.html", Application.StartupPath);
            String server = "http://erp.hispay.com.cn";
            String local = "http://localhost:8888";
            _browser = new ChromiumWebBrowser(server);
            _browser.KeyboardHandler = new KeyBoardHander();
            _browser.DownloadHandler = new DownloadHander();
            _browser.TitleChanged += (s, ee) =>
            {
                BeginInvoke(new Action(() => { 
                    this.Text = ee.Title;
                }));
            };
            _browser.LoadError += (s, ee) =>
            {
                Console.WriteLine(s);
                Console.WriteLine(ee.ErrorCode);
                BeginInvoke(new Action(() =>
                {
                    if (ee.ErrorCode.ToString().Equals("InternetDisconnected"))
                    {
                        Console.WriteLine("网络错误");
                    }
                    else if (ee.ErrorCode.ToString().Equals("ConnectionRefused"))
                    {
                        Console.WriteLine("服务器未打开");
                    }
                }));
            };
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            CefSharpSettings.WcfEnabled = true;
            _browser.JavascriptObjectRepository.Register("BizBrowser", new JsInterface(_browser, this), isAsync: false, options: BindingOptions.DefaultBinder);
            this.panel1.Controls.Add(_browser);
            _browser.Dock = DockStyle.Fill;

        }

        /***
         * 主窗体线程之外调用
         * 进行Invoke
         * 解决子窗体dialog问题
         * */
        delegate void ShowAclasSend(String ips,String pluLines);
        public void AclasSend(String ips, String pluLines)
        {
            if (this.InvokeRequired)
            {
                ShowAclasSend st = new ShowAclasSend(AclasSend);
                this.Invoke(st, new object[] { ips,pluLines });
            }
            else
            {
               AclasSendForm p = new AclasSendForm(ips, pluLines);
               p.ShowDialog();
            }

        }

        /***
         * 主窗体线程之外调用
         * 进行Invoke
         * 解决子窗体dialog问题
         * */
        delegate void ShowPreview(String tempName, String hfJson, String detailJson);
        public void Preview(String tempName,String hfJson,String detailJson)
        {
            if (this.InvokeRequired)
            {
                ShowPreview st = new ShowPreview(Preview);
                this.Invoke(st, new object[] { tempName, hfJson,detailJson });
            }
            else
            {
                Preview p = new Preview(tempName, hfJson, detailJson);
                p.ShowDialog();
            }

        }

        /***
        * 主窗体线程之外调用
        * 进行Invoke
        * 解决子窗体dialog问题
        * */
        delegate string ShowPrint(String tempName, String hfJson, String detailJson);
        public string Print(String tempName, String hfJson, String detailJson)
        {
            if (this.InvokeRequired)
            {
                ShowPrint st = new ShowPrint(Print);
                return (String)this.Invoke(st, new object[] { tempName, hfJson, detailJson });
            }
            else
            {
                PrintSetting ps = new PrintSetting(tempName,hfJson,detailJson);
                ps.ShowDialog();
                Console.Write(ps.getJsonReuslt());
                return ps.getJsonReuslt();
            }

        }

        /***
       * 主窗体线程之外调用
       * 进行Invoke
       * 解决子窗体dialog问题
       * */
        delegate void ShowDesign(String tempName);
        public void Design(String tempName)
        {
            if (this.InvokeRequired)
            {
                ShowDesign st = new ShowDesign(Design);
                this.Invoke(st, new object[] { tempName });
            }
            else
            {
                Design des = new Design("test.frx");
                des.ShowDialog();
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Cef.Shutdown();
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {
            VersionForm vf = new VersionForm();
            vf.ShowDialog();
        }
    }
}
